#pragma once

#include<iostream>
#include<string>
#include<unordered_map>
#include<fstream>
#include"Log.hpp"
using namespace std;
namespace dict_ns
{
    const string defaultpath="Dict.txt";
    const string sep=": ";

    class Dict
    {
    private:
        bool Load()
        {
            ifstream in(_dict_conf_filepath);//????
            if(!in.is_open())
            {
                LOG(FATAL,"open %s error\n",_dict_conf_filepath.c_str());
                return false;
            }
            string line;
            while(getline(in,line))
            {
                if(line.empty())    continue;
                auto pos=line.find(sep);//happy: 开心的
                string word=line.substr(0,pos);
                if(word.empty())    continue;
                string china=line.substr(pos+sep.size());
                if(china.empty())   continue;
                LOG(DEBUG,"load info %s: %s\n",word.c_str(),china.c_str());
                _dict.insert(make_pair(word,china));
            }
            in.close();
            LOG(DEBUG,"load %s success\n",_dict_conf_filepath.c_str());
            return true;
        }
    public:
        Dict(const string &path=defaultpath):_dict_conf_filepath(path)
        {   
            Load();
        }
        string Translate(const string &word,bool &ok)
        {
            ok=true;
            auto iter=_dict.find(word);
            if(iter==_dict.end())
            {
                ok=false;
                return " ";
            }
            return iter->second;
        }
        ~Dict()
        {

        }
    private:
        unordered_map<string,string> _dict;
        string _dict_conf_filepath;
    };
}